package controller

import (
	pb "../protobuf"
	"golang.org/x/net/context"
	"net"
	"strings"
	"fmt"
)

// 初始化
func init() {
	initLocalIp()
}

// 本地ip
var localIp string

// 初始化
func initLocalIp() {
	network, address := "tcp", "163.com:80"
	conn, err := net.Dial(network, address)
	if err != nil {
		fmt.Printf("net.Dial(%v, %v) error : %v \n", network, address, err)
	}
	defer conn.Close()
	localIp = strings.Split(conn.LocalAddr().String(), ":")[0]
}

// Test
type Test struct{}

// SayHello
func (s *Test) SayHello(ctx context.Context, in *pb.HelloRequest) (r *pb.HelloReply, err error) {
	// 本地ip为空，获取本地ip
	if localIp == "" {
		initLocalIp()
	}
	// 响应
	r = &pb.HelloReply{
		Name: in.Name,
		Ip:   localIp,
	}
	fmt.Printf("SayHello success : %#v \n", r)
	return r, err
}
